home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windows News 2010 Summer - Disc 1
/
WN_Ete2010_CD1.iso
/
Onglet5
/
Weezo
/
Weezo setup.exe
/
{code_appDir}
/
www
/
includes
/
uploadWindow.php
< prev
next >
Wrap
PHP Script
|
2010-05-19
|
18KB
|
427 lines
<?php
/**
* File upload script, executed in a popup window
* 2 Steps upload :
* - 1st step : display "upload in progress" windows, create upload in upload.txt text file, and call doUploadFile JavaScript in opener window
* - 2nd step : doUploadFile script submits file upload form in pop-up window
*
* Display "please wait" screen
* Upload a file to server, and check authorizations
*
* If no file is sent, display "please wait" screen and call doUploadFile script in opener window
* This script submit file send form, targeting current widows
* file reception is then detected.
*
* PHP version 5
*
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
*
* @category NA
* @package NA
* @author Nicolas Bruley / Peer 2 World <contact@weezo.net>
* @copyright 2005-2009 Nicolas Bruley / Peer 2 World
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id:$
* @link http://www.weezo.net
* @since File available since Release 1.0.0
*/
require_once(INCLUDE_DIR.'outputFunctions.php');
require_once(INCLUDE_DIR.'transferFunctions.php');
require_once(INCLUDE_DIR.'explorerFunctions.php');
/**
* @desc : display Upload In Progress (or error) page (main upload page)
*
*/
function displayUploadInProgressPage(){
// Set close URL back to resource for frameless display
$_ENV['winCloseURL']='/res/'.cfRGetVar('type').'/'.cfRGetVar('subType').'/'.cfRGetVar('baseFile');
$message=cfCaption('uploadInProgress',basename($_POST['preUlFile']))."\n".cfCaption('genWait');
// Add upload to transfers list
if(cfRGetVar('trackTransfers')){
if(!$tr=tAddUpload(cfUTF8Decode($_POST['preUlDir']), cfUTF8Decode(basename($_POST['preUlFile'])))) {
$message=cfCaption('genError');
$error=1;
}
}
// Async request (mobile)
if(cfIsAsync()){
cfAsyncHeader();
// Proceed to uploadForm submission
if(!isset($error)) echo cfAsyncXMLJSaction('doUploadFile("'.((isset($tr))?$tr->id:'').'")');
// or display error message
else echo cfAsyncXMLJSaction('initUploadProceed("forbidden","'.$message.'")');
die(cfAsyncFooter());
}
// Display page
cfInsertHEAD(false);
?>
</head>
<?php
if(!cfRGetVar('trackTransfers') || (cfRGetVar('trackTransfers') && $tr)) {
if(isset($tr)) $transferId=$tr->id; else $transferId=rand();
// On page loaded, call upload javascript funtion in opener window
if(cfHGetVar('noPopup')) $opener='parent.frames[parent.dgi(winMe.getOpenerId()+\'_if\').name]'; else $opener='opener';
echo '<body onload="'.$opener.'.doUploadFile(\''.$transferId.'\')">';
}
else echo '<body>';
echo outDivFrame('frame1','id="frame1"').'<div class="frame1header">'.cfCaption('genFileUpload').'</div>';
// Upload message
echo '<br/><br/><center>'.$message."<br/><br/>\n";
// Progress bar iframe
if(isset($transferId)) echo '<iframe id="uploadProgressIFrame" src="upload.php?progressBar=1&transferId='.$transferId.'" width="80%" height="17" frameborder="0" scrolling="no"></iframe>';
else echo '<br/>';
echo "</center><br/><br/>\n";
insertCloseOnCompletedControl();
?>
</div>
<script type="text/javascript">
winMe.closeable(false);
winMe.reloadable(false);
winMe.resizeInnerTo(<?php echo (cfGetBrowser()=='gecko')?600:350; // Gecko pb ?>,dgi('frame1').offsetHeight*1+10*actualOffsetTop(dgi('frame1')),1)
</script>
</body>
</html>
<?php
exit;
}
/**
* @desc Insert control needed for toggleing close transfer on upload completed
*
*/
function insertCloseOnCompletedControl(){
echo '<form id="testForm" name="testForm" action="'.$_SERVER['PHP_SELF'].'">';
echo '<input type="checkbox" '.((cfRGetVar('transferCloseOnCompleted'))?'checked="checked"':'').' onclick="dgn(\'toggleTransferCloseOnCompleted\').value=this.checked;asyncSubmitForm(\'testForm\',true)"><input name="toggleTransferCloseOnCompleted" type="text" style="display:none"/> ';
echo cfCaption('transferCloseOnFinish');
echo '</form>';
}
/**
* @desc : display Upload Completed page
*
* @param string $errorMessage
*/
function displayUploadCompletedPage($uploadFile=false,$errorMessage=false){
/**
* Mobile devices
*/
if(cfIsMobile()){
require_once(INCLUDE_DIR.'mobileFunctions.php');
mfInsertHead(array('context'=>'view'));
echo outDivFrame('frame1').'<div class="frame1header">'.cfCaption('genFileUpload').'</div><center>';
// If transfered file is an image, display a thumbnail
if(efFileType($uploadFile)=='image') $isImage=true; else $isImage=false;
if(!$errorMessage && $isImage) {
if(cfRGetVar('path') && cfIsSubDir(cfRGetVar('path'),$uploadFile)) $uploadFileExtPath=cfResourceRelativePath($uploadFile);
elseif (cfIsSubDir(cfAppResourceDir(),$uploadFile)) $uploadFileExtPath=cfResourceDataDirRelativePath($uploadFile);
else $uploadFileExtPath=$uploadFile;
echo '<img src="'.cfExtImage($uploadFileExtPath,80,80,false).'" height="80" alt=""><br/>';
}
else echo "<br/><br/>\n";
// Message
if($errorMessage) echo $errorMessage;
else echo cfCaption('transfersFinished');
if(!$isImage) echo "<br/>\n";
echo '<br/><br/></center></div></body></html>';
exit;
}
// Flash upload: don't display HTML page
if(isset($_POST['flashUpload'])){
if(!$uploadFile) cfHTTPError(403);
exit;
}
// Set close URL back to resource for frameless display
$_ENV['winCloseURL']='/res/'.cfRGetVar('type').'/'.cfRGetVar('subType').'/'.cfRGetVar('baseFile');
// Display page
cfInsertHEAD(false);
?>
<script language="javascript">
function keyPressed(e) {
if (e.keyCode) keycode=e.keyCode; else keycode=e.which;
if(keycode==27) winMe.closeMe();
}
winMe.closeable(true);
winMe.reloadable(false);
</script>
</head>
<body <?php
if(isset($_POST['ulAckJSFunction'])){
echo 'onload="';
// If parent window/frame exists, inform parent that upload is finished
if(cfTGetVar('frames')){
if(cfHGetVar('noPopup')) $opener='parent.frames[parent.dgi(winMe.getOpenerId()+\'_if\').name]';
else $opener='opener';
echo $opener.'.'.$_POST['ulAckJSFunction'].'('.(($errorMessage)?'false':'true').');';
}
if(!$errorMessage && cfRGetVar('transferCloseOnCompleted')) echo 'winMe.closeMe();';
echo '"';
}
echo ' onKeyPress="keyPressed(event);">';
echo outDivFrame('frame1').'<div class="frame1header">'.cfCaption('genFileUpload').'</div>';
echo '<center>';
if(efFileType($uploadFile)=='image') $isImage=true; else $isImage=false;
// If transfered file is an image, display a thumbnail
if(!$errorMessage && $isImage) {
if(cfRGetVar('path') && cfIsSubDir(cfRGetVar('path'),$uploadFile)) $uploadFileExtPath=cfResourceRelativePath($uploadFile);
elseif (cfIsSubDir(cfAppResourceDir(),$uploadFile)) $uploadFileExtPath=cfResourceDataDirRelativePath($uploadFile);
else $uploadFileExtPath=$uploadFile;
echo '<img src="'.cfExtImage($uploadFileExtPath,80,80,false).'" height="80" alt=""><br/>';
}
else echo "<br/><br/>\n";
// Message
if($errorMessage) echo $errorMessage;
else echo cfCaption('transfersFinished');
if(!$isImage) echo "<br/>\n";
// Close button
echo '<br/>'.outButton(cfCaption('genClose'),"javascript:winMe.closeMe()",outIcon('close'));
echo '</center>';
if($errorMessage || !$isImage) echo '<br/><br/>';
insertCloseOnCompletedControl();
echo '</div>';
echo '</body></html>';
// Remove temporary file
if(isset($_FILES['ulInput']['tmp_name']) && file_exists($_FILES['ulInput']['tmp_name'])) @unlink($_FILES['ulInput']['tmp_name']);
exit();
}
/**
* @desc default uploaded file processing
*
*/
function uploadProcessUploadedFile(){
global $transfersArray;
// Prepare to move uploaded file to resource directory
$uploadDir=str_replace('*resourceBasePath*',cfRGetVar('path'),cfUTF8Decode($_POST['ulDir']));
//$uploadFile=cfJoinPathFile($uploadDir, basename(cfUTF8Decode($_FILES['ulInput']['name'])));
$uploadFile=cfJoinPathFile($uploadDir, cfUTF8Decode(basename($_POST['postUlFile'])));
// Check destination directory access rights
if(!cfFileRights($uploadDir,'upload')) displayUploadCompletedPage(false,$uploadDir.cfCaption('explorerErrorFileSend2').' (UL NA)');
// If resource sets an extension filter, verify that file matches
if(cfRGetVar('extensionFilter') && !in_array(cfFileExtension($uploadFile),cfRGetVar('extensionFilter'))) displayUploadCompletedPage(false,cfCaption('explorerFormatError').' (ext2)');
// If resource sets an file type filter, verify that file matches
if(cfRGetVar('fileTypeFilter') && !in_array(efFileType($uploadFile),cfRGetVar('fileTypeFilter'))) displayUploadCompletedPage(false,cfCaption('explorerFormatError')).' (filter2)';
// User may write in destination dir
else {
if(file_exists($uploadFile) && !cfFileRights($uploadFile,'modify')) displayUploadCompletedPage(false,cfCaption('explorerErrorFileSend2').' (modify not allowed)');
else {
if(file_exists($uploadFile)) {
if(cfFileRights($uploadFile,'modify')){
if(!customSendToRecycled($uploadFile)) displayUploadCompletedPage(false,cfCaption('explorerErrorFileSend2').' (err deleting prev file)');
}
else displayUploadCompletedPage(false,cfCaption('explorerErrorFileSend2').' (modify not allowed2)');
}
// Folder is write-allowed => move uploaded file to folder
if(@move_uploaded_file($_FILES['ulInput']['tmp_name'], $uploadFile)){
// Transfers tracking : set upload as finished
if(cfRGetVar('trackTransfers')){
$transfersArray[$_POST['ulTransferId']]->status=4;
$transfersArray[$_POST['ulTransferId']]->progress=1;
// Save result in uploads file
tWritePHPTransfersFile($transfersArray);
}
// Remove temporary file
if(isset($_FILES['ulInput']['tmp_name']) && file_exists($_FILES['ulInput']['tmp_name'])) @unlink($_FILES['ulInput']['tmp_name']);
}
else{
// Remove temporary file
if(isset($_FILES['ulInput']['tmp_name']) && file_exists($_FILES['ulInput']['tmp_name'])) @unlink($_FILES['ulInput']['tmp_name']);
// Transfers tracking : set upload as failed
if(cfRGetVar('trackTransfers')){
$transfersArray[$_POST['ulTransferId']]->status=8;
$transfersArray[$_POST['ulTransferId']]->progress=1;
// Save result in uploads file
tWritePHPTransfersFile($transfersArray);
}
// Move uploaded file (unexpected) failure
displayUploadCompletedPage(false,cfCaption('explorerErrorFileSend2').' (cannot move uploaded file)');
}
}
}
// Display upload completed page
displayUploadCompletedPage($uploadFile);
}
/**
* @desc Display upload progress bar
* This page is called within an iframe of main upload page
*
*/
function uploadProgressBar(){
/**
* Display page
*/
if(isset($_GET['transferId']) && !isset($_POST['asyncRequest'])){
$transferId=$_GET['transferId'];
cfInsertHEAD(false);
?>
<body onload="updateProgressBar('<?php echo $transferId; ?>',0,100)" style="margin:0;padding:0">
<script type="text/javascript">
function updateProgressBar(transferId,perc,nr){
progressSetPerc('uploadProgressBar',perc);
progressSetText('uploadProgressBar',Math.floor(perc)+'%');
window.setTimeout('sendData("transferId='+transferId+'&progressBar=1","upload.php")',nr);
}
function cancelUpload(){
parent.document.stop();
}
</script>
<center id="center">
<?php
// Insert progress bar
echo outProgressBar(0,'95%','0%','uploadProgressBar',false,false,'margin:0');
echo '</center></body>';
}
/**
* Async refresh
*/
else{
$updateInterval=1;
$progress=0;
$transferId=$_POST['transferId'];
cfAsyncHeader();
/**
* Upload progress
*/
//if(function_exists('apc_fetch')) $status=apc_fetch('upload_'.$transferId); // Using APC
//elseif(function_exists('uploadprogress_get_info')) $status=uploadprogress_get_info($transferId); // Using uploadprogress
if(function_exists('customUploadMonitor')) $status=customUploadMonitor($transferId); // Using uploadprogress
else $status=false;
//cfDbg($_POST+array($transferId=>$status),1);
if($status){
// Convert from customProgressMonitor format to APC format
if(isset($status['transfered'])) $status['current']=$status['transfered'];
// Convert from uploadprogress format to APC format
if(isset($status['bytes_total'])) $status['total']=$status['bytes_total'];
if(isset($status['bytes_uploaded'])) $status['current']=$status['bytes_uploaded'];
// Check if upload is not > to max upload size
if(isset($status['total']) && $status['total'] > cfGGetVar('uploadMaxFilesize')*1024*1024){
echo cfAsyncXMLJSaction('dgi("center").innerHTML="'.cfCaptionJS('configUploadMax').cfCaptionJS('genSeparator').cfGGetVar('uploadMaxFilesize').' '.cfCaptionJS('genSizeMb').'"; cancelUpload()');
}
// Load transers
require_once(INCLUDE_DIR.'transferFunctions.php');
$transfers=tReadTransfersFiles();
// Update transfer progress
if(isset($transfers[$transferId])){
$tr=new transfer('upload');
$tr=$transfers[$transferId];
// Progressive update interval increase
if($tr->updateInterval<0.2) $tr->updateInterval+=0.01;
elseif($tr->updateInterval<1) $tr->updateInterval+=0.1;
elseif($tr->updateInterval<3) $tr->updateInterval+=1;
$updateInterval=$tr->updateInterval;
// Update progress
if(isset($status['current']) && isset($status['total']) && $status['total']>0) {
$progress=$tr->updateProgress($status['current']/$status['total'],false);
}
// Save to file
$tr->commitToFile($transfers);
}
}
// Update HTML progress bar
echo cfAsyncXMLJSaction('updateProgressBar("'.$transferId.'",'.(100*$progress).','.(1000*$updateInterval).')');
echo cfAsyncFooter();
}
exit;
}
$transfersArray=array();
$errorMessage=false;
/*
***************************************************************************************************************************
* Close-on-upload completed toggle
***************************************************************************************************************************
*/
if(isset($_POST['toggleTransferCloseOnCompleted'])) {
cfRSetVar('transferCloseOnCompleted',(($_POST['toggleTransferCloseOnCompleted']=='true')?true:false));
exit;
}
/*
***************************************************************************************************************************
* Upload progress page
***************************************************************************************************************************
*/
if((isset($_GET['progressBar']) && isset($_GET['transferId'])) || (isset($_POST['progressBar']) && isset($_POST['transferId']))) uploadProgressBar();
/*
***************************************************************************************************************************
* Pre-Upload processing
***************************************************************************************************************************
*/
if(!count($_FILES) && isset($_POST['preUlFile']) && isset($_POST['preUlDir'])) {
// If resource sets an extension filter, verify that file matches
if(cfRGetVar('extensionFilter') && !in_array(cfFileExtension(cfUTF8Decode($_POST['preUlFile'])),cfRGetVar('extensionFilter'))) displayUploadCompletedPage(false,cfCaption('explorerFormatError').' (ext not allowed)');
// If resource sets an file type filter, verify that file matches
if(cfRGetVar('fileTypeFilter') && !in_array(efFileType(cfUTF8Decode($_POST['preUlFile'])),cfRGetVar('fileTypeFilter'))) displayUploadCompletedPage(false,cfCaption('explorerFormatError').' (type not allowed)');
// If file not yet sent : display "upload in progress" page and launch upload
displayUploadInProgressPage();
}
/*
***************************************************************************************************************************
* Process completed download
***************************************************************************************************************************
*/
// If missing POST parameters, display error message
if(!isset($_POST['ulDir']) || !isset($_POST['ulTransferId'])) {
displayUploadCompletedPage(false,cfCaption('genError').'<br/>'.cfCaption('configUploadMax').cfCaption('genSeparator').cfGGetVar('uploadMaxFilesize').' '.cfCaption('genSizeMb'));
}
// Check passed file has been uploaded
if($_FILES['ulInput']['error']) displayUploadCompletedPage(false,cfCaption('explorerFormatError').' (err:'.$_FILES['ulInput']['error'].')');
if(empty($_FILES['ulInput']['tmp_name'])) displayUploadCompletedPage(false,cfCaption('explorerFormatError').' (empty tmp_name)');
if(!is_uploaded_file($_FILES['ulInput']['tmp_name'])) displayUploadCompletedPage(false,cfCaption('explorerFormatError').' (!is_uploaded_file)');
// Verify that transfer is present in upload.txt file and that it has not been stopped (except for frameless upload where transfer is not logged)
if(cfRGetVar('trackTransfers')){
$transfersArray=tReadTransfersFiles();
if(!isset($transfersArray[$_POST['ulTransferId']]) || $transfersArray[$_POST['ulTransferId']]->status!=3) displayUploadCompletedPage(false,cfCaption('transfersCanceled').' (canceled)');
}
// Process uploaded file
if(function_exists('processUploadedFile')) processUploadedFile();
else uploadProcessUploadedFile();
?>